package com.zou.uaa.security.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zou.uaa.vo.UserVo;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

@Component
public class SuccessHandler implements AuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        httpServletResponse.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        UserVo userVo = new UserVo();
        userVo.setToken("sakldgjklasdjglaslknzxS216DG416AS1DG651AS6D5G16AS");
        userVo.setNikName("oauth2.0测试");
        HashMap hashMap = new HashMap();
        hashMap.put("code", 200);
        hashMap.put("data", userVo);
        hashMap.put("msg", "登录成功");
        httpServletResponse.getWriter().write(new ObjectMapper().writeValueAsString(hashMap));
    }

}
